/**
 * SysUserServiceImpl.java -- 2017-10-16 17:18:36
 * 作者：欧增奇 -- 代码生成器自动生成
 * 联系方式：425752797@qq.com
 * http://www.ouzengqi.com/
 */
package com.ozq.sys.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ozq.common.page.Page;
import com.ozq.common.page.PageUtil;
import com.ozq.sys.dao.entity.SysUser;
import com.ozq.sys.dao.mapper.SysUserMapper;
import com.ozq.sys.service.inter.SysUserService;

/**
 * <系统-用户表接口实现> <功能详细描述>
 * 
 * @author 欧增奇
 * @version [V2.0, 2017-10-16 17:18:36]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService
{
    static Logger logger = LoggerFactory.getLogger(SysUserServiceImpl.class);

    /**
     * 系统-用户表Mapper[SysUser]
     */
    @Autowired
    private SysUserMapper sysUserMapper;

    /** ------------------------------------公共-工具------------------------------------ **/
    /**
     * 分页公共实现
     */
    @Override
    public Page pageManage(Page page)
    {
        logger.debug("debug page: {}", page);

        // 默认每页10条记录
        if (page.getEveryPage() <= 0)
            page.setEveryPage(10);

        // 默认第一页
        if (page.getCurrentPage() <= 0)
            page.setCurrentPage(1);

        return PageUtil.createPage(page.getEveryPage(), page.getTotalCount(), page.getCurrentPage());
    }

    /**
     * 缓存清理
     */
    @Override
    public void clearCache()
    {
        logger.debug("清理系统-用户表缓存成功:{}", "sysUser");
    }

    /** ------------------------------------公共-业务------------------------------------ **/
    /** --公共-业务-统计-- **/
    /**
     * 统计系统-用户表所有的记录数
     * 
     * @return int
     */
    @Override
    public int countAll()
    {
        return sysUserMapper.countAll();
    }

    /**
     * 按照条件精确统计系统-用户表
     * 
     * @param sysUser
     * @return int
     */
    @Override
    public int countBy(SysUser sysUser)
    {
        logger.debug("debug sysUser: {}", sysUser);

        if (sysUser == null)
            return 0;

        return sysUserMapper.countBy(sysUser);
    }

    /**
     * 按照条件模糊统计系统-用户表
     * 
     * @param sysUser
     * @return int
     */
    @Override
    public int countLike(SysUser sysUser)
    {
        logger.debug("debug sysUser: {}", sysUser);

        if (sysUser == null)
            return 0;

        return sysUserMapper.countLike(sysUser);
    }

    /** --公共-业务-添加-- **/
    /**
     * 插入系统-用户表
     * 
     * @param sysUser
     * @return sysUser
     */
    @Override
    public SysUser add(SysUser sysUser)
    {
        logger.debug("debug sysUser: {}", sysUser);

        if (sysUser == null)
            return null;

        sysUserMapper.insert(sysUser);
        return sysUser;
    }

    /**
     * 插入系统-用户表集合
     * 
     * @param sysUser
     * @return boolean
     */
    @Override
    public boolean addList(List<SysUser> sysUsers)
    {
        logger.debug("debug sysUsers: {}", sysUsers);

        if (sysUsers == null || sysUsers.isEmpty())
            return false;

        List<SysUser> inserts = filterEmpty(sysUsers);
        if (inserts.isEmpty())
            return false;

        sysUserMapper.insertList(inserts);
        return true;
    }

    /**
     * 批量插入系统-用户表，当id存在更新这条数据，不存在插入这条数据（注意：会影响所有字段）
     * 
     * @param sysUsers
     * @return boolean
     */
    @Override
    public boolean addUpdateList(List<SysUser> sysUsers)
    {
        logger.debug("debug sysUsers: {}", sysUsers);

        if (sysUsers == null || sysUsers.isEmpty())
            return false;

        List<SysUser> inserts = filterEmpty(sysUsers);
        if (inserts.isEmpty())
            return false;

        sysUserMapper.insertUpdateList(inserts);
        return true;
    }

    /** --公共-业务-查询-- **/
    /**
     * 按照系统-用户表查询,如果存在id，则用id查询(本方法只会返回一条数据，当查询有1条以上返回null)
     * 
     * @param sysUser
     * @return sysUser
     */
    @Override
    public SysUser find(SysUser sysUser)
    {
        logger.debug("debug sysUser: {}", sysUser);

        if (sysUser == null)
            return null;
        
        if (sysUser.getUserId() != null)
            return sysUserMapper.selectById(sysUser.getUserId());

        sysUser.setStartLine(0);
        sysUser.setEndLine(1);
        List<SysUser> list = sysUserMapper.listBy(sysUser);
        if (list == null || list.isEmpty())
            return null;

        return list.get(0);
    }

    /**
     * 按照id查询系统-用户表
     * 
     * @param userId
     * @return sysUser
     */
    @Override
    public SysUser findById(Integer userId)
    {
        logger.debug("debug userId: {}", userId);

        if (userId == null)
            return null;

        return sysUserMapper.selectById(userId);
    }

    /**
     * 按照ids查询系统-用户表
     * 
     * @param userIds
     * @return sysUser集合
     */
    @Override
    public List<SysUser> findByIds(List<Integer> userIds)
    {
        logger.debug("debug userIds: {}", userIds);

        if (userIds == null || userIds.isEmpty())
            return null;

        return sysUserMapper.selectByIds(userIds);
    }

    /**
     * 按照条件精确查询系统-用户表返回集合方法
     * 
     * @param sysUser
     * @return sysUser
     */
    @Override
    public List<SysUser> listBy(SysUser sysUser)
    {
        logger.debug("debug sysUser: {}", sysUser);

        if (sysUser == null)
            return null;

        return sysUserMapper.listBy(sysUser);
    }

    /**
     * 按照条件模糊查询系统-用户表返回集合方法
     * 
     * @param sysUser
     * @return sysUser集合
     */
    @Override
    public List<SysUser> listLike(SysUser sysUser)
    {
        logger.debug("debug sysUser: {}", sysUser);

        if (sysUser == null)
            return null;

        return sysUserMapper.listLike(sysUser);
    }

    /**
     * 按照分页条件精确查询系统-用户表返回集合方法
     * 
     * @param sysUser
     * @return sysUser集合
     */
    @Override
    public List<SysUser> listPageBy(Page page, SysUser sysUser)
    {
        logger.debug("debug page: {}", page);
        logger.debug("debug sysUser: {}", sysUser);

        if (sysUser == null)
            sysUser = new SysUser();

        if (page != null)
        {
            sysUser.setStartLine(page.getBeginIndex());
            sysUser.setEndLine(page.getEveryPage());
        }
    
        if (sysUser.getOrderBy() == null)
            sysUser.setOrderBy(SysUser.FIELD_USER_REGISTER_TIME + SysUser.ORDER_BY_DESC);

        return sysUserMapper.listBy(sysUser);
    }

    /**
     * 按照分页条件模糊查询系统-用户表返回集合方法
     * 
     * @param sysUser
     * @return sysUser集合
     */
    @Override
    public List<SysUser> listPageLike(Page page, SysUser sysUser)
    {
        logger.debug("debug page: {}", page);
        logger.debug("debug sysUser: {}", sysUser);

        if (sysUser == null)
            sysUser = new SysUser();

        if (page != null)
        {
            sysUser.setStartLine(page.getBeginIndex());
            sysUser.setEndLine(page.getEveryPage());
        }
    
        if (sysUser.getOrderBy() == null)
            sysUser.setOrderBy(SysUser.FIELD_USER_REGISTER_TIME + SysUser.ORDER_BY_DESC);

        return sysUserMapper.listLike(sysUser);
    }

    /** --公共-业务-修改-- **/
    /**
     * 修改系统-用户表
     * 
     * @param sysUser
     * @return boolean
     */
    @Override
    public boolean update(SysUser sysUser)
    {
        logger.debug("debug sysUser: {}", sysUser);

        if (sysUser == null || sysUser.getUserId() == null)
            return false;

        sysUserMapper.update(sysUser);
        return true;
    }

    /**
     * 批量修改系统-用户表
     * 
     * @param sysUsers
     * @return boolean
     */
    @Override
    public boolean updateList(List<SysUser> sysUsers)
    {
        logger.debug("debug sysUsers: {}", sysUsers);

        if (sysUsers == null)
            return false;

        List<SysUser> news = new ArrayList<SysUser>();
        for (SysUser sysUser : sysUsers)
            if (sysUser != null && sysUser.getUserId() != null)
                news.add(sysUser);

        if (news.isEmpty())
            return false;

        sysUserMapper.updateList(news);
        return true;
    }

    /** --公共-业务-删除-- **/
    /**
     * 指定id删除
     * 
     * @param userId
     * @return boolean
     */
    @Override
    public boolean deleteById(Integer userId)
    {
        logger.debug("debug userId: {}", userId);

        if (userId == null)
            return false;

        sysUserMapper.deleteById(userId);
        return true;
    }

    /**
     * 批量删除指定ids
     * 
     * @param userId
     * @return boolean
     */
    @Override
    public boolean deleteByIds(List<Integer> userIds)
    {
        logger.debug("debug userIds: {}", userIds);

        if (userIds == null || userIds.isEmpty())
            return false;

        sysUserMapper.deleteByIds(userIds);
        return true;
    }

    /**
     * 按照条件精确删除系统-用户表
     * 
     * @param sysUser
     * @return boolean
     */
    @Override
    public boolean deleteBy(SysUser sysUser)
    {
        logger.debug("debug sysUser: {}", sysUser);

        if (sysUser == null)
            return false;

        sysUserMapper.deleteBy(sysUser);
        return true;
    }

    /**
     * 按照条件模糊删除系统-用户表
     * 
     * @param sysUser
     * @return boolean
     */
    @Override
    public boolean deleteLike(SysUser sysUser)
    {
        logger.debug("debug sysUser: {}", sysUser);

        if (sysUser == null)
            return false;

        sysUserMapper.deleteLike(sysUser);
        return true;
    }

    /** --公共-业务-自定义-- **/
    /**
     * 自定义方法
     * 
     * @param sysUser
     * @return map集合
     */
    @Override
    public List<Map<String, Object>> custom(SysUser sysUser)
    {
        logger.debug("debug sysUser: {}", sysUser);

        // 自己实现自己的业务
        // sysUserMapper.selectCustom(selectSQL);
        return null;
    }

    /** ------------------------------------私有-方法------------------------------------ **/
    /**
     * 过滤参数为空
     * 
     * @param sysUsers
     * @return sysUser集合
     */
    private List<SysUser> filterEmpty(List<SysUser> sysUsers)
    {
        List<SysUser> news = new ArrayList<SysUser>();
        for (SysUser sysUser : sysUsers)
            if (sysUser != null)
                news.add(sysUser);
        return news;
    }
}